/*************************
//DEMO 1bit SMD 7SEG LED DISPLAY （未测试）
//Hardware Interface:
//LED DISPLAY<--ULN2003A<--MCU
//共阳数码管<--ULN2003A<--单片机 出1 数码管量
*************************/
#define EK_STC51  //Using EK-STC51 Learning Platform,if not Commented it out with '//'

#ifdef EK_STC51   
	#include "STC8A8K64D4.H"//EK-STC51 Head file
#else  
	#include <reg51.h>
#endif
#include "intrins.h"
 
sbit KEY1=P3^2;//按键KEY1 显示数字+1
sbit KEY2=P3^3;//按键KEY2 显示数字-1

#define pSMD_DIG P2 //数码管接P2口  SMD Dignum PORT  a~g <->io:P2.7~P2.1
#define pSMD_DIG_OFF 0xFF
unsigned char code p1SMDDigFontTable[]={  // COM GND mode code 共阴数码管）的段码
0xFC,
0x60,
0xda,
0xf2,
0x66,
0xb6,
0xbe,
0xe0,
0xfe,
0xf6,
0x00 //off
};


void Delay500ms(void)	//EK-STC51 @11.0592MHz
{
#ifdef EK_STC51   
	unsigned char data i, j, k;
	i = 29;
	j = 14;
	k = 54;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
#else  
	unsigned char i, j;
	i = 15;
	j = 58;
	do
	{
		while (--j);
	} while (--i);
#endif	
}



void pSMD_DIG_InitDisplay()
{
	unsigned char i;
	for(i=0;i<10;i++)
	{
		pSMD_DIG=~p1SMDDigFontTable[i];
		Delay500ms();
	}
}


void InitGPIOMode(void)
{
#ifdef EK_STC51 
	//设置IO为双向模式--可读（输入）和可写（输出）
	P0M0 = 0x00;                                 
    P0M1 = 0x00;
	P1M0 = 0x00;                                 
    P1M1 = 0x00;
	P2M0 = 0x00;                                 
    P2M1 = 0x00;
	P3M0 = 0x00;                                 
    P3M1 = 0x00;
#endif
}

void Init_HW()
{
#ifdef EK_STC51 
	InitGPIOMode();
#endif
	pSMD_DIG=pSMD_DIG_OFF; //Init OFF the DIGnum display
}  

//上电数码管显示0~9

void main()
{	
	char key_num=0;
	Init_HW();
	pSMD_DIG_InitDisplay();//上电数码管现实0-9
	while(1)
	{
		if(KEY1==0)
		{
			Delay500ms();
			if(KEY1==0)
			{
				 	if(++key_num>9)key_num=0;
					pSMD_DIG=p1SMDDigFontTable[key_num];
			}
		}
		if(KEY2==0)
		{
			Delay500ms();
			if(KEY2==0)
			{
			 		if(--key_num<0)key_num=9;
					pSMD_DIG=p1SMDDigFontTable[key_num];	
			}
		}
		
	}
}